﻿using AutoDeployForSims.Utility;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AutoDeployForSims.Service
{
    public class VSDPCtrlService
    {
        private readonly string Pattern = @"COM\d{0,2}"; // \d 匹配任何数字  

        public bool CreateVSDPCom(string com1Name, string com2Name)
        {
            bool isRight = false;
            //校验参数格式为COMX
            if (string.IsNullOrEmpty(com1Name) || string.IsNullOrEmpty(com2Name))
            {
                return isRight;
            }
            com1Name = com1Name.ToUpper();
            com2Name = com2Name.ToUpper();
            if (!Regex.IsMatch(com1Name, Pattern) || !Regex.IsMatch(com2Name, Pattern))
            {
                return isRight;
            }
            try
            {
                if (VSPDTool.CreatePair(com1Name, com2Name))
                {
                    isRight = WriteRegedit(com1Name, com2Name);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }

            return isRight;
        }

        public bool DeleteVSDPCom(string comName)
        {
            bool isRight = false;
            //校验参数格式为COMX
            if (string.IsNullOrEmpty(comName))
            {
                return isRight;
            }
            comName = comName.ToUpper();
            if (!Regex.IsMatch(comName, Pattern))
            {
                return isRight;
            }
            try
            {
                isRight = VSPDTool.DeletePair(comName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return isRight;
        }

        public bool WriteRegedit(string comName1, string comName2)
        {
            bool isRight = false;
            try
            {
                // 打开VSBC7服务下的Ports键  
                using (RegistryKey servicesKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\VSBC7", true))
                {
                    if (servicesKey == null)
                    {
                        throw new Exception("Unable to open VSBC7 service key.");
                    }

                    // 打开或创建Ports键  
                    RegistryKey portsKey = servicesKey.OpenSubKey("Ports", true) ?? servicesKey.CreateSubKey("Ports");

                    // 假设我们要添加的端口名为COM5和COM6  
                    string port1Name = comName1.ToUpper();
                    string port2Name = comName2.ToUpper();
                    string portPairName = port1Name + port2Name;

                    // 在Ports键下创建一个新的子键，用于存储端口对的信息  
                    using (RegistryKey portPairKey = portsKey.CreateSubKey(portPairName))
                    {
                        // 设置端口1和端口2的名称  
                        portPairKey.SetValue("Port1", port1Name);
                        portPairKey.SetValue("Port2", port2Name);

                        // 设置StrictBaudrate为1（启用严格的波特率仿真）  
                        portPairKey.SetValue("StrictBaudrate", 1, RegistryValueKind.DWord);

                        // 设置Wiring值（这里仅为示例，您需要根据实际情况设置正确的值）  
                        byte[] wiringBytes = { 0xA0, 0x10, 0x00, 0x00 };
                        portPairKey.SetValue("Wiring", wiringBytes, RegistryValueKind.Binary);

                        Console.WriteLine("Port pair has been added successfully.");
                        isRight = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }

            return isRight;
        }
    }
}
